﻿using System;

namespace Sail.Caching
{
    /// <summary>
    /// 弱引用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Weak<T>
    {
        //弱引用对象
        private readonly WeakReference _target;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="target">被弱引用的对象</param>
        public Weak(T target)
        {
            _target = new WeakReference(target);
        }
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="target">被弱引用的对象</param>
        /// <param name="trackResurrection">是否使用指定的复活跟踪</param>
        public Weak(T target, bool trackResurrection)
        {
            _target = new WeakReference(target, trackResurrection);
        }

        /// <summary>
        /// 弱引用目标对象
        /// </summary>
        public T Target
        {
            get { return (T)_target.Target; }
            set { _target.Target = value; }
        }
    }
}
